package com.example.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.util.function.Tuples;

import java.time.Duration;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 服务器推送事件
 * Created by mingge on 2018/5/4.
 *
 *
 * 注解添加服务器推送消息失败
 */
@RestController
@RequestMapping
@Api(tags = "test flux")
public class SseController {
//    @ApiOperation("test flux numbers")
//    @GetMapping("/randomNumbers")
//    public Flux<ServerSentEvent<Integer>> randomNumbers() {
//        System.out.println("SseController.randomNumbers");
//        return Flux.interval(Duration.ofSeconds(1))
//                .map(seq -> Tuples.of(seq, ThreadLocalRandom.current().nextInt()))
//                .map(data -> ServerSentEvent.<Integer>builder()
//                        .event("random")
//                        .id(Long.toString(data.getT1()))
//                        .data(data.getT2())
//                        .build());
//    }
}